package com.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heart.common.constant.AppConstant;
import com.heart.common.utils.Md5Utils;
import com.heart.common.utils.ObjectUtils;
import com.heart.common.utils.RandomUtils;
import com.heart.dao.entity.SecurityUser;
import com.heart.dao.entity.Token;
import com.heart.dao.mapper.SecurityUserMapper;
import com.heart.dao.mapper.TokenMapper;
import com.heart.service.SecurityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SecurityUserServiceImpl extends ServiceImpl<SecurityUserMapper, SecurityUser> implements SecurityUserService {

    @Autowired
    private SecurityUserMapper securityUserMapper;
    @Autowired
    private TokenMapper tokenMapper;

    public String getSalt(String userCode){
        QueryWrapper<SecurityUser> wrapper = new QueryWrapper<>();
        wrapper.eq("tc_security_user_code",userCode);
        wrapper.eq("tc_security_user_useflag",AppConstant.ONE);
        SecurityUser securityUser = securityUserMapper.selectOne(wrapper);
        if(ObjectUtils.isNull(securityUser)){
            return AppConstant.EMPTY;
        }
        return securityUser.getSecuritySalt();
    }

    public Map<String, Object> info(String token){
        Token t = tokenMapper.selectById(token);
        if(ObjectUtils.isNull(t)){
            return null;
        }
        QueryWrapper<SecurityUser> wrapper = new QueryWrapper<>();
        wrapper.eq("tc_security_user_code",t.getSecurityUserCode());
        wrapper.eq("tc_security_user_useflag",AppConstant.ONE);
        SecurityUser securityUser = securityUserMapper.selectOne(wrapper);
        if(ObjectUtils.isNull(securityUser)){
            return null;
        }

        HashMap<String, Object> result = new HashMap<>();
        List<String> roles = new ArrayList<>();
        roles.add("admin");
        result.put("roles", roles);
        result.put("introduction", "I am a super administrator");
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        result.put("name", securityUser.getSecurityUserName());
        return result;
    }

    public Map<String,Object> login(String userCode, String password) {
        Map<String,Object> res = new HashMap<>();

        QueryWrapper<SecurityUser> wrapper = new QueryWrapper<>();
        wrapper.eq("tc_security_user_code",userCode);
        wrapper.eq("tc_security_user_useflag",AppConstant.ONE);
        SecurityUser securityUser = securityUserMapper.selectOne(wrapper);
        if(ObjectUtils.isNull(securityUser)){
            return null;
        }
        String lastStr = Md5Utils.md5(securityUser.getSecurityUserPasswod() + securityUser.getSecuritySalt());
        if(password.equals(lastStr)){
            String token = RandomUtils.simpleUuid();
            Token t = new Token();
            t.setToken(token);
            t.setSecurityUserCode(securityUser.getSecurityUserCode());
            t.setCreateTime(new Date());
            tokenMapper.insert(t);
            res.put("token", token);

            return res;
        }
        return null;
    }

    public List<SecurityUser> getSecurityUserList(){
        QueryWrapper<SecurityUser> wrapper = new QueryWrapper<>();
        wrapper.eq("tc_security_user_useflag",AppConstant.ONE);
        List<SecurityUser> securityUsers = securityUserMapper.selectList(wrapper);
        return securityUsers;
    }

    public boolean updateSecurityUser(SecurityUser securityUser){
        UpdateWrapper<SecurityUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("tc_security_user_code",securityUser.getSecurityUserCode());
        wrapper.set("tc_security_user_name", securityUser.getSecurityUserName());
        return securityUserMapper.update(securityUser,wrapper) > 0;
    }

    public boolean deleteSecurityUser( SecurityUser securityUser){
        UpdateWrapper<SecurityUser> wrapper = new UpdateWrapper();
        wrapper.eq("tc_security_user_code", securityUser.getSecurityUserCode());
        wrapper.set("tc_security_user_useflag", securityUser.getSecurityUserUseflag());
        return securityUserMapper.update(null, wrapper) > 0;
    }

    public boolean saveSecurityUser(SecurityUser securityUser){
        securityUser.setSecurityUserUseflag(AppConstant.ONE);
        securityUser.setSecuritySalt(RandomUtils.randomNumber());

        return securityUserMapper.insert(securityUser) > 0;
    }

    public  boolean updateSecurityUserPassword(SecurityUser securityUser){
        UpdateWrapper<SecurityUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("tc_security_user_code",securityUser.getSecurityUserCode());
        wrapper.set("tc_security_user_passwod", securityUser.getSecurityUserPasswod());
        return securityUserMapper.update(securityUser,wrapper) > 0;
    }
}
